<template>
  <div>
    <el-card class="box-card">
      <el-form :model="form" :inline="true" label-width="100px">
        <el-form-item label="时间:">
          <el-date-picker v-model="form.createTime" type="datetime" placeholder="选择一个日期" format="YYYY/MM/DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss " />
        </el-form-item>
        <el-form-item label="客户姓名:">
          <el-input v-model="form.clientName" />
        </el-form-item>
        <el-form-item label="客户电话:">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <div class="button_div">
        <span class="Order_title">部门订单</span>
      </div>
      <el-table v-loading="loading" :data="tableData.list" :default-sort="{ prop: 'date', order: 'descending' }" style="width: 100%">
        <el-table-column prop="createTime" label="订单创建时间" sortable width="160" />
        <el-table-column prop="clientName" label="客户姓名" width="100" />
        <el-table-column prop="phone" label="客户电话" width="160" />
        <el-table-column prop="partner" label="合作方" width="120" />
        <el-table-column prop="orderManager" label="客户经理" width="120" />
        <el-table-column prop="orderStatus" label="订单状态" width="160" />
        <el-table-column prop="reserveAmount" label="定金金额" width="120" />
        <el-table-column prop="amount" label="合同金额" width="120" />
        <el-table-column prop="delayed" label="是否延期" width="160">
          <template #default="scope">
          <div v-if="scope.row.delayed==='延期'" style="color: red;">
            {{ scope.row.delayed }}
          </div>
          <div v-else >
            {{ scope.row.delayed }}
          </div>
        </template>
        </el-table-column>
        <el-table-column prop="address" label="地址" width="120" />
        <el-table-column fixed="right" label="操作" width="150">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row, '编辑')">详情
            </el-button>
            <el-popconfirm width="200px" confirm-button-text="是的" cancel-button-text="不" :icon="InfoFilled" icon-color="#626AEF" title="确认删除?" @confirm="confirmEvent(scope.$index, scope.row)" @cancel="cancelEvent">
              <template #reference>
                <el-button size="small" type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination 
      background 
      :page-sizes="[ 5, 10, 20]" 
      layout="total, sizes, prev, pager, next"   
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange" 
      :total="tableData.total" />
    </el-card>
    <DialogForm ref="dialogRef" @updateForm="updateForm" :orderId="orderid"></DialogForm>
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { tbOrderDelete, tbOrderListData} from '/@/api/tb/tbOrder'
import { ElMessage } from 'element-plus'
import DialogForm from './components/DialogForm.vue'
import { InfoFilled } from '@element-plus/icons-vue'
import {useUserStore} from "../../../store/modules/user"
  const tableData = ref([]);
  const userstore=useUserStore().userInfo
  let orderid = reactive();
  const showAdd=ref(false)
const dialogRef = ref<any>()
const loading = ref(true)
const form = reactive({
  clientName: '',
  createTime: '',
  phone: '',
  pageSize: 10,
  pageNo: 1
})

const getListData = async () => {
  loading.value=true
  const res = await tbOrderListData(form)
    if(res.list){
      loading.value=false
      tableData.value = res
    }
}

getListData()
 // 获取到当前用户的角色信息
 const GetUserRole = async () => {
    for(let i=0;i<userstore?.roleList?.length;i++){
       if(userstore?.roleList[i].roleName=="供应商"){
        showSupplier.value=true;
       }else if(userstore?.roleList[i].roleName=="销售主管"){
        showAdd.value=true;
       }else if(userstore?.roleList[i].roleName=="销售人员"){
        showAdd.value=true;
       }else if(userstore?.roleList[i].roleName=="设计主管"){
        showAdd.value=true;
       }
    }
  };

  GetUserRole();

const handleEdit = (index: number, row, title: string) => {
  dialogRef.value.Open(title, row.orderId)
}
const updateForm=()=>{
  getListData()
}
const confirmEvent = async (index: number, row) => {
  const res = await tbOrderDelete({ orderId: row.orderId })
  if (res.result) {
    ElMessage({
      message: '删除' + res.message,
      type: 'success'
    })
  } else {
    ElMessage.error('删除' + res.message)
  }
  getListData()
}
const cancelEvent = () => {
  ElMessage('取消删除')
}

const OpenDialog = query => {
  dialogRef.value.Open(query)
}
const onSubmit = async () => {
  getListData()
}



const handleSizeChange = (val: number) => {
  form.pageSize=val
  getListData()
}
const handleCurrentChange = (val: number) => {
  form.pageNo=val
  getListData()
}
</script>
<style lang="less" scoped>
.box-card {
  margin-bottom: 20px;
  border-radius: 10px;
}
.button_div {
  width: 100%;
  display: flex;
  justify-content: space-between;
  .Order_title {
    padding: 5px 10px;
    font-size: 18px;
  }
}
.el-pagination{
   display: flex;
   justify-content: flex-end;
}
</style>
